#pragma once

#include "StdAfx.h"
#include "Sensor.h"

CSensor::CSensor(void)
{
	m_oldpos = m_pos = CPoint(0,0);
	m_Temperature = 0.0;
	m_Humidity = 0.0;
	m_Co2 = 0.0;
	m_OILevel = 0;
	m_stinks = 0;
	m_bMove = false;
	m_func.TEMPERATURE = TRUE;
	m_func.CO2 = FALSE;
	m_func.OI = FALSE;
	m_bConnected = false;
	m_strLocation = L"";
	m_Co2Alram = 0;
	m_oldAliveCnt = 0;
	m_aliveCnt = 0;
	m_timeoutCnt = 0;
}

CSensor::CSensor(CPoint point)
{
	m_oldpos = m_pos = point;
	m_Temperature = 0.0;
	m_Humidity = 0.0;
	m_Co2 = 0.0;
	m_OILevel = 0;
	m_stinks = 0;
	m_bMove = false;
	m_func.TEMPERATURE = TRUE;
	m_func.CO2 = FALSE;
	m_func.OI = FALSE;
	m_bConnected = false;
	m_strLocation = L"";
	m_Co2Alram = 0;
	m_oldAliveCnt = 0;
	m_aliveCnt = 0;
	m_timeoutCnt = 0;
}

CSensor::~CSensor(void)
{
}

bool CSensor::selectCheck(CPoint pos)
{
	if(m_pos.x+20 > pos.x && m_pos.x < pos.x 
		&& m_pos.y+20 > pos.y && m_pos.y < pos.y )
		return true;
	else
		return false;
}

void CSensor::initData()
{
	m_Temperature = 0.0;
	m_Humidity = 0.0;
	m_Co2 = 0.0;
	m_OILevel = 0;
	m_stinks = 0;
	m_Co2Alram = 0;
}

bool CSensor::getTimeOut()
{
	bool bTimeout = false;

	if(m_bConnected){
		if(m_timeoutCnt == 10){
			bTimeout = true;
			m_timeoutCnt = 0;
			m_bConnected = false;
		}
		else{
			if(m_oldAliveCnt == m_aliveCnt)
				m_timeoutCnt++;
			else
				m_oldAliveCnt = m_aliveCnt;
		}
	}

	return bTimeout;
}

void CSensor::setAlive()
{
	if(m_aliveCnt == 100)
		m_aliveCnt = 0;

	m_aliveCnt++;
}